package com.potato369.cryptEureka.repository;

import com.potato369.cryptEureka.domain.Permission;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface PermissionRepository extends JpaRepository<Permission, Long> {

    @Query(value = "SELECT p.* FROM ums_admin_role_relation AS ar LEFT JOIN ums_role AS r ON ar.role_id=r.id LEFT JOIN ums_role_permission_relation AS rp ON r.id=rp.role_id LEFT JOIN ums_permission AS p ON rp.permission_id=p.id WHERE ar.admin_id=:adminId AND p.id IS NOT NULL AND p.id NOT IN(SELECT p.id FROM ums_admin_permission_relation as pr left join ums_permission AS p ON pr.permission_id=p.id WHERE pr.type=-1 AND pr.admin_id=:adminId) UNION SELECT p.* FROM ums_admin_permission_relation pr LEFT JOIN ums_permission p ON pr.permission_id=p.id WHERE pr.type=1 AND pr.admin_id=:adminId", nativeQuery = true)
    List<Permission> getPermissionList(@Param("adminId") Long adminId);
}
